package com.icat.house.web.intercept;

import com.icat.house.model.UserInfo;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

public class AdminInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation ai) {
		Controller controller = ai.getController();
		String token = controller.getPara("token");
		if (!StrKit.notBlank(token)) {
			token = controller.getSessionAttr("token");
		}
		if (StrKit.notBlank(token) || !"null".equals(token)) {
			UserInfo info = UserInfo.validateToken(token);
			if (StrKit.notNull(info)) {
				controller.setAttr("user", info);
				ai.invoke();
				return;
			} else {
				controller.forwardAction("/admin/login");
				return;
			}
		} else {
			controller.forwardAction("/admin/login");
			return;
		}
	}
}
